﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Abp.Authorization;
using Abp.Authorization.Users;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.Organizations;
using Abp.Runtime.Caching;
using WebApp.Authorization.Roles;
using Abp.Dapper.Repositories;
using System.Threading.Tasks;

namespace WebApp.Authorization.Users
{
    public class UserManager : AbpUserManager<Role, User>
    {
        IDapperRepository<User,Guid> _userDapperRepository;
        public UserManager(
            RoleManager roleManager,
            UserStore store, 
            IOptions<IdentityOptions> optionsAccessor, 
            IPasswordHasher<User> passwordHasher, 
            IEnumerable<IUserValidator<User>> userValidators, 
            IEnumerable<IPasswordValidator<User>> passwordValidators,
            ILookupNormalizer keyNormalizer, 
            IdentityErrorDescriber errors, 
            IServiceProvider services, 
            ILogger<UserManager<User>> logger, 
            IPermissionManager permissionManager, 
            IUnitOfWorkManager unitOfWorkManager, 
            ICacheManager cacheManager, 
            IRepository<OrganizationUnit, Guid> organizationUnitRepository, 
            IRepository<UserOrganizationUnit, Guid> userOrganizationUnitRepository, 
            IOrganizationUnitSettings organizationUnitSettings, 
            ISettingManager settingManager,
            IDapperRepository<User, Guid> userDapperRepository)
            : base(
                roleManager, 
                store, 
                optionsAccessor, 
                passwordHasher, 
                userValidators, 
                passwordValidators, 
                keyNormalizer, 
                errors, 
                services, 
                logger, 
                permissionManager, 
                unitOfWorkManager, 
                cacheManager,
                organizationUnitRepository, 
                userOrganizationUnitRepository, 
                organizationUnitSettings, 
                settingManager)
        {

            _userDapperRepository = userDapperRepository;
        }
        /// <summary>
        /// 获取所有自己创建的创建的用户Id
        /// </summary>
        public List<Guid> GetOwnCreaterUserIds()
        {
            var Data = _userDapperRepository.Query($@"  WITH T
            AS(
                SELECT Id FROM[AbpUsers] WHERE Id = '{AbpSession.UserId.ToString()}'
                UNION ALL
                SELECT U.Id
                FROM[AbpUsers] U INNER JOIN T ON U.[CreatorUserId] = T.Id
            )
            SELECT * FROM T");
            List<Guid> UserIds = new List<Guid>();
            foreach(var t in Data)
            {
                UserIds.Add(t.Id);
            }
            return UserIds;

        }
        /// <summary>
        /// 获取所有自己有用的角色
        /// </summary>
        public async Task<List<string>> GetOwnRoles()
        {
            var user = await GetUserByIdAsync(AbpSession.UserId ?? Guid.Empty); //登录 人信息
            if (user == null) return null;
            var ownRole = await GetRolesAsync(user);//拥有的角色
            return (List<string>)ownRole;

        }
    }
}
